<?php

namespace app\common\model\transit;
use app\common\model\user\User;
use think\facade\Session;
use app\api\controller\Pub;
use think\Model;
class Transitcompany extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = 'transitcompany';
        protected $pk = 'TransitCompanyId';
        
        const STATEINT    = 1;//初始化
        const STATEOK    = 1;//没问题的
        const STATELOCK   = 2; //锁定
        const STATEDELETE = 404;//被删除的

    public function userlist()
    {
        return $this->hasMany('TransitUser','TransitCompanyId','TransitCompanyId');
    }

    /**
     * 状态分类
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public static function getStatusText($value = '')
    {
        $status = [-1=>'全部企业',1=>'正常企业',2=>'锁定企业'];
        if($value!==''){
            return $status[$value];
        }
        return $status;
    }
    
    /**
     * 创建企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_create($data,$type='tran'){
        $userlogininfo = Pub::getuserid();
        $userid = $userlogininfo['msg'];
        $company = new Transitcompany;
        if($type=='tran'){
            $company->UserId = $userid;
        } 

        $company->Name = $data['Name'];
        $company->Tel = $data['Tel'];

        $company->Lon = $data['Lon'];
        $company->Lat = $data['Lat'];
        $company->Address = $data['Address'];

        $company->State = Transitcompany::STATEINT;
        $company->TransitNumber = $data['TransitNumber'];
        $company->save();
        return $company->TransitCompanyId;
    }

    
     /**
     * 身份证字段默认隐藏
     * @param  [type] $value [description]
     * @return [type]        [description]
     */
     public static function getUserCardAttr($value)
    {   
        if(!empty($value)){
            return substr($value, 0 , 4).'****'.substr($value, -4);
        }else{
            return '';
        }
    }

    /**
     * 获取信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function user_getinfo($cid='',$userid = '',$hidden=['ReInfo']){
       
        if(!empty($cid)){
            $data = Transitcompany::where('TransitCompanyId',$cid)->find();
        }else{
            if(empty($userid)){
                $userlogininfo = Pub::getuserid();
                $userid = $userlogininfo['msg'];
            }
            $data = Transitcompany::where('UserId',$userid)->find();
        }
        if(empty($data)){
            return false;
        }
        $data->hidden($hidden);
        return $data;
    }

    /**
     * 用户修改企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_reinfo($data,$type='tran'){
        if($type=='tran'){
            $company = self::user_getinfo();
        }else if($type=='mana'){
            $company = self::user_getinfo($data['id']);
        }

        $reinfo = empty($company->OperationLog)?'':$company->OperationLog."\n";
        
        $userinfo = User::getinfo();
        $reinfo = $reinfo .'【' .$userinfo->Name.'|'.$userinfo->UserId.'】在'.date('Y-m-d H:i',time()).'进行了信息调整：';
        $nreinfo = '';
        foreach ($data as $key => $value) {
            $sb = $company->getfiledname($key);
            if(empty($sb)){
                continue;
            }
            if(!empty($value)&&$company->$sb !== $value){
                $nreinfo =  $nreinfo . '字段【'.$company->getfiledname($key,2).'】由【'.$company->$sb .'】修改为【' .$value.'】;'."\n";
                //输入来的参数名非字段名需要进行替换
                $company->$sb = $value;
            }
        }
        if(!empty($nreinfo)){
            $reinfo = $reinfo ."\n" . $nreinfo;
            $company->OperationLog = $reinfo;
            $company->save();
        }
    }

    /**
     * 获取订单列表
     * @param  [type] $data    [主数据]
     * @param  string $type    [查询类型/hawa/buss]
     * @param  [type] $is_page [是否分页显示]
     * @return [type]          [description]
     */
    public static function usergetlist($data,$type = 'tran',$is_page = true){
        $userinfo = User::getinfo();
        if($type=='tran'){
            $rdata = Transitcompany::where('State',Transitcompany::STATEOK);
            $visible = ['TransitCompanyId','TransitNumber','Tel','Name','Img','Lon','Lat','Address','State','UserId'];
            if(!empty($data['field'])){
                $visible = array_merge($data['field'],$visible);
            }
        }else if($type=='mana'){
            $rdata = Transitcompany::where('State',Transitcompany::STATEOK);
            $rdata = $rdata->with(['userlist']);
            $visible = ['TransitCompanyId','TransitNumber','Tel','Name','Img','State','Lon','Lat','Address','userlist','UserId'];
            if(!empty($data['field'])){
                $visible = array_merge($data['field'],$visible);
            }
        }else{
            return moerr('缺少参数');
        }

        $option = come_model_option($data,$rdata,$is_page);
        
        $rdata = $option['rdata'];
        $data = $option['data'];
        
        if(!empty($data['field'])){
            $rdata = $rdata->field($visible);
        }
        $rdata = $rdata->select();

        if(empty($data['field'])){
            $rdata =  $rdata->visible($visible);
        }

        $ndata['list'] = $rdata->toArray();
        //为处置场的关联用户绑定数据
        if($type=='mana'){
            $idlist = get_array_id($ndata,'UserId','list','userlist');
            $sblist = User::where('UserId','in',$idlist)->field('UserId,State,Name,Iphone')->select();
            $userlist = [];
            foreach ($sblist as $key => $value) {
                $userlist[$value['UserId']] = $value;
            }
            foreach ($ndata['list'] as $key => $value) {
                foreach ($value['userlist'] as $key2 => $value2) {
                    if(!empty($userlist[$value2['UserId']])){
                        $ndata['list'][$key]['userlist'][$key2]['Name'] = $userlist[$value2['UserId']]['Name'];
                        $ndata['list'][$key]['userlist'][$key2]['State'] = $userlist[$value2['UserId']]['State'];
                        $ndata['list'][$key]['userlist'][$key2]['Iphone'] = $userlist[$value2['UserId']]['Iphone'];
                    }else{
                        $ndata['list'][$key]['userlist'][$key2]['Name'] ='没有此用户';
                        $ndata['list'][$key]['userlist'][$key2]['State'] = User::STATELOCK;
                        $ndata['list'][$key]['userlist'][$key2]['Iphone'] ='';
                    }
                }
            }
            $ndata['userstatetext'] = User::getStatusText();
        }

        $ndata['statetext'] = Transitcompany::getStatusText();
        // $goodid = get_array_id($ndata,'GoodId','list','listinfo');
        if($is_page){
            $ndata['pageinfo'] = ['pageindex'=>$data['pageindex'],'pagemax'=>$data['count']];
        }
        return $ndata;
    }

    public static function getfiledname($str,$type=1){
        $filename = ['Tel'=>'紧急联系电话',
                 'Name'=>'公司名称','TransitNumber'=>'营运证号',
                 'Lon'=>'精度','Lat'=>'维度','Address'=>'地址',
                 'PlanUserName'=>'平台账号','PlanPassword'=>'平台密码',
                 'PlanAuthTime'=>'授权时间'];
        if($type==1){
            if(empty($filename[$str])){
                return false;
            }
            return $str;
        }else{
            if(empty($filename[$str])){
                return false;
            }
            return $filename[$str];
        }
    }

}
